package com.auemall.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.auemall.constant.ApplicationConstant;
import com.auemall.dao.TBuyerPOMapper;
import com.auemall.dao.TDeliverinfoPOMapper;
import com.auemall.dao.TOrderPOMapper;
import com.auemall.dao.TOrderPOMapper_extend;
import com.auemall.dao.TOrderitemPOMapper;
import com.auemall.po.TBuyerPO;
import com.auemall.po.TDeliverinfoPO;
import com.auemall.po.TOrderPO;
import com.auemall.po.TOrderPO_extend;
import com.auemall.po.TOrderitemPO;
import com.auemall.po.TOrderitemPOExample;
import com.auemall.service.OrderService;
import com.auemall.utils.EUDataGridResult;
import com.auemall.utils.IDUtils;
import com.auemall.utils.StringUtils;
import com.auemall.vo.BaseRespVO;
import com.auemall.vo.OrderDetailRespVO;
import com.auemall.vo.OrderDetailVO;
import com.auemall.vo.OrderRespVO_extend;
import com.auemall.vo.OrderVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class OrderServiceImpl implements OrderService {
    
    private Logger logger = Logger.getLogger(OrderServiceImpl.class);

    @Autowired
    private TOrderPOMapper orderPOMapper;                //订单信息表
    
    @Autowired
    private TOrderitemPOMapper orderitemPOMapper;        //订单明细表
    
    @Autowired
    private TOrderPOMapper_extend orderPOMapper_extend;  //订单扩展表

    @Autowired
    private TBuyerPOMapper buyerPOMapper;                //会员信息表
    
    @Autowired
    private TDeliverinfoPOMapper deliverinfoPOMapper;    //邮寄地址信息表
    
    
    @Override
    public EUDataGridResult selectOrderList(OrderVO req, int page, int rows) throws Exception {
        EUDataGridResult respVO = new EUDataGridResult();
        if(null == req){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        Map<String, Object> params = new HashMap<String, Object>();
        if(StringUtils.isNotBlank(req.getUsername())){
            params.put("username", req.getUsername());
        }
        if(StringUtils.isNotBlank(req.getOrderid())){
            params.put("orderid", req.getOrderid());
        }
        if(StringUtils.isNotBlank(req.getCreatedatebegin())){
            params.put("createdatebegin", req.getCreatedatebegin());
        }
        if(StringUtils.isNotBlank(req.getCreatedateend())){
            params.put("createdateend", req.getCreatedateend());
        }
        if(StringUtils.isNotBlank(req.getRecipients())){
            params.put("recipients", req.getRecipients());
        }
        if(StringUtils.isNotBlank(req.getNote())){
            params.put("note", req.getNote());
        }
        PageHelper.startPage(page, rows);
        
        List<OrderRespVO_extend> respList = new ArrayList<OrderRespVO_extend>();
        
        List<TOrderPO_extend> list = orderPOMapper_extend.selectListByMap(params);
        
        if(null != list && list.size() > 0){
            for(TOrderPO_extend po : list){
                OrderRespVO_extend vo = new OrderRespVO_extend();
                BeanUtils.copyProperties(po, vo);
                if(po.getCreatedate() != null){
                    vo.setCreatedate(ApplicationConstant.dateTimeFormat.format(po.getCreatedate()));
                }
                respList.add(vo);
            }
        }
        respVO.setRows(respList);
        //取记录总条数
        PageInfo<TOrderPO_extend> pageInfo = new PageInfo<>(list);
        respVO.setTotal(pageInfo.getTotal());
        respVO.setResult(ApplicationConstant.RESULT_SUCCESS);
        respVO.setMsg("查询订单列表成功");
        return respVO;
    }

    @Override
    @Transactional
    public BaseRespVO addOrder(OrderVO req) throws Exception {
        BaseRespVO respVO = new BaseRespVO();
        if(null == req){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        TOrderPO record = new TOrderPO();
        BeanUtils.copyProperties(req, record);
        Long orderId = IDUtils.genItemId();
        record.setOrderid(orderId.toString());
        orderPOMapper.insertSelective(record);
        respVO.setResult(ApplicationConstant.RESULT_SUCCESS);
        respVO.setMsg("新增订单成功");
        return respVO;
    }

    @Override
    @Transactional
    public BaseRespVO updateOrder(OrderVO req) throws Exception {
        BaseRespVO respVO = new BaseRespVO();
        if(null == req){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        if(StringUtils.isBlank(req.getOrderid())){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数[OrderID]为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        TOrderPO record = new TOrderPO();
        BeanUtils.copyProperties(req, record);
        orderPOMapper.updateByPrimaryKeySelective(record);
        respVO.setResult(ApplicationConstant.RESULT_SUCCESS);
        respVO.setMsg("修改订单成功");
        return respVO;
    }

    @Override
    @Transactional
    public BaseRespVO deleteOrder(OrderVO req) throws Exception {
        BaseRespVO respVO = new BaseRespVO();
        if(null == req){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        if(StringUtils.isBlank(req.getOrderid())){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数[OrderID]为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        orderPOMapper.deleteByPrimaryKey(req.getOrderid());
        respVO.setResult(ApplicationConstant.RESULT_SUCCESS);
        respVO.setMsg("删除订单成功");
        return respVO;
    }

    @Override
    public OrderDetailRespVO selectOrderDetail(OrderVO req) throws Exception {
        OrderDetailRespVO respVO = new OrderDetailRespVO();
        if(null == req){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        if(StringUtils.isBlank(req.getOrderid())){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数[OrderID]为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        //1 查询订单明细列表
        List<OrderDetailVO> respList = new ArrayList<OrderDetailVO>();
        TOrderitemPOExample example = new TOrderitemPOExample();
        TOrderitemPOExample.Criteria criteria = example.createCriteria();
        criteria.andOrderIdEqualTo(req.getOrderid());
        List<TOrderitemPO> list = orderitemPOMapper.selectByExample(example);
        if(null != list && list.size() > 0){
            for(TOrderitemPO po : list){
                OrderDetailVO vo = new OrderDetailVO();
                BeanUtils.copyProperties(po, vo);
                respList.add(vo);
            }
        }
        respVO.setList(respList);
        //2 查询会员信息
        if(null != req.getBuyid()){
            TBuyerPO buyerPO = buyerPOMapper.selectByPrimaryKey(req.getBuyid());
            if(null != buyerPO){
                BeanUtils.copyProperties(buyerPO, respVO);
            }
        }
        //3 查询邮寄地址信息
        if(null != req.getDeliverId()){
            TDeliverinfoPO deliverinfoPO = deliverinfoPOMapper.selectByPrimaryKey(req.getDeliverId());
            if(null != deliverinfoPO){
                respVO.setDeliver_address(deliverinfoPO.getAddress());
                respVO.setDeliver_email(deliverinfoPO.getEmail());
                respVO.setDeliver_gender(deliverinfoPO.getGender());
                respVO.setDeliver_mobile(deliverinfoPO.getMobile());
                respVO.setDeliver_postalcode(deliverinfoPO.getPostalcode());
                respVO.setDeliver_recipients(deliverinfoPO.getRecipients());
                respVO.setDeliver_requirement(deliverinfoPO.getRequirement());
                respVO.setDeliver_tel(deliverinfoPO.getTel());
            }
        }
        
        respVO.setResult(ApplicationConstant.RESULT_SUCCESS);
        respVO.setMsg("查询订单详情成功");
        return respVO;
    }

    @Override
    @Transactional
    public BaseRespVO deleteOrderDetail(String id) throws Exception {
        BaseRespVO respVO = new BaseRespVO();
        if(StringUtils.isBlank(id)){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数id为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        orderitemPOMapper.deleteByPrimaryKey(Integer.parseInt(id));
        respVO.setResult(ApplicationConstant.RESULT_SUCCESS);
        respVO.setMsg("删除订单详情成功");
        return respVO;
    }

}
